package com.zkingsoft.web.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.matrix.core.anotations.RemoveRequestToken;
import com.matrix.core.anotations.SaveRequestToken;
import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.StringUtils;
import com.matrix.core.tools.WebUtil;
import com.zkingsoft.common.bean.SysUsers;
import com.zkingsoft.web.bean.WebAdcolumn;
import com.zkingsoft.web.service.WebAdcolumnService;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2016-12-20 15:50
 */
@Controller
@RequestMapping(value = "admin/webAdcolumn")
public class WebAdcolumnController {

	@Resource
	private WebAdcolumnService webAdcolumnService;

	// 记录编辑前的值Before_Edit_Value
	public static final String BEV = "WebAdcolumn_BEV";

	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(WebAdcolumn webAdcolumn, PaginationVO pageVo) {
		if (pageVo == null) {
			pageVo = new PaginationVO();
		}
		SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
		webAdcolumn.setCompanyId(user.getCompanyId());
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS,
				webAdcolumnService.findInPage(webAdcolumn, pageVo), webAdcolumnService.findTotal(webAdcolumn));
		return result;
	}

	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/all")
	public @ResponseBody AjaxResult all() {
		SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
		WebAdcolumn webAdcolumn=new WebAdcolumn();
		webAdcolumn.setCompanyId(user.getCompanyId());
		return new AjaxResult(AjaxResult.STATUS_SUCCESS, webAdcolumnService.findByModel(webAdcolumn));
	}

	/**
	 * 新增或者修改页面
	 */
	@RemoveRequestToken
	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(WebAdcolumn webAdcolumn) {
		if (webAdcolumn.getColId() != null) {
			int i = webAdcolumnService.modifyByMap(WebUtil.getSessionAttribute(BEV), webAdcolumn);
			WebUtil.removeSessionAttribute(BEV);
			if (i > 0) {
				return new AjaxResult(AjaxResult.STATUS_SUCCESS, "广告栏修改成功");
			} else {
				return new AjaxResult(AjaxResult.STATUS_FAIL, "广告栏修改失败");
			}
		} else {
			SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
			webAdcolumn.setCompanyId(user.getCompanyId());
			int i = webAdcolumnService.add(webAdcolumn);
			if (i > 0) {
				return new AjaxResult(AjaxResult.STATUS_SUCCESS, "广告栏新增成功");
			} else {
				return new AjaxResult(AjaxResult.STATUS_FAIL, "广告栏新增失败");
			}
		}
	}

	/**
	 * 进入修改界面
	 */
	@SaveRequestToken
	@RequestMapping(value = "/editForm")
	public String editForm(Long id) {
		WebAdcolumn webAdcolumn;
		if (id != null) {
			webAdcolumn = webAdcolumnService.findById(id);
			WebUtil.getRequest().setAttribute("obj", webAdcolumn);
			WebUtil.setSessionAttribute(BEV, webAdcolumn);
		}
		return "admin/web/adColumn-form";
	}

	/**
	 * 删除
 */
	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		List<Long> ids = StringUtils.strToCollToLong(keys, ",");
		int i = webAdcolumnService.remove(ids);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS, "成功删除" + i + "条数据");
		} else {
			return new AjaxResult(AjaxResult.STATUS_FAIL, "删除失败");
		}
	}
	
	/**
	 * 显示备注去重
	 */
	@RequestMapping(value = "/page")
	public @ResponseBody AjaxResult kb() {
		return new AjaxResult(AjaxResult.STATUS_SUCCESS, webAdcolumnService.findByRemark());
	}

}